package com.hmyd.hmyd_android.shop;

import java.lang.reflect.Type;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Address;
import com.hmyd.hmyd_android.dto.DtoInfoNoPage;
import com.hmyd.hmyd_android.dto.InfoDto;
import com.hmyd.hmyd_android.ui.BasicActivity;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.WheelAddressPopupwindow;
import com.hmyd.hmyd_android.view.WheelAddressPopupwindow.OnSelectedListener;
import com.hmyd.hmyd_android.view.WheelPopupListener;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 新增收货地址
 * @author wangk
 */
@ContentView(R.layout.activity_jf_mall_add_new_address)
public class JfMallAddNewAddressActivity extends BasicActivity implements
		OnSelectedListener {

	private static final int POST_NEW_ADDRESS = 1;
	private static final int POST_EDIT_ADDRESS = 2;
	/*
	 * 验证手机号正则表达式
	 */

	@ViewInject(R.id.newRecName)
	private EditText newRecName;
	@ViewInject(R.id.newRecAddress)
	private TextView newRecCity;
	@ViewInject(R.id.newRecDetailAddress)
	private EditText newDetailAddress;
	@ViewInject(R.id.newRecphone)
	private EditText recPhone;
	@ViewInject(R.id.newRecFixPhone)
	private EditText recFixPhone;
	@ViewInject(R.id.rootView)
	private View rootView;
	@ViewInject(R.id.home_title_txt)
	private TextView home_title_txt;
	@ViewInject(R.id.home_right)
	private TextView home_right;
	@ViewInject(R.id.addAddressTitle)
	private View addAddressTitle;

	private WheelAddressPopupwindow addressPopWindow;

	private Address address;

	private Address editAddress;

	@OnClick({R.id.newRecAddress})
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.newRecAddress:
			addressPopWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
			break;
		default:
			break;
		}
	}

	private WheelPopupListener popListener = new WheelPopupListener() {

		@Override
		public void getSecondString(String name) {

		}

		@Override
		public void getSecondString(int index) {

		}

		@Override
		public void getFirstString(String name) {

		}

		@Override
		public void getFirstString(int index) {

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onPostSuccess(int postId, String msg) {
		Gson gson = new Gson();
		switch (postId) {
		case POST_NEW_ADDRESS:
			if (msg != null && !msg.trim().equals("")) {
				DtoInfoNoPage<InfoDto> dtoInfoNoPage = new DtoInfoNoPage<InfoDto>();
				Type type1 = new TypeToken<DtoInfoNoPage<InfoDto>>() {}.getType();
				dtoInfoNoPage = gson.fromJson(msg, type1);
				if(dtoInfoNoPage != null) {
					String backCode = String.valueOf(dtoInfoNoPage.suc);
					InfoDto data =dtoInfoNoPage.data;
					String info = data.info;
					if (backCode != null && Integer.parseInt(backCode) == 1011) {
						MyToast.showHMToast(JfMallAddNewAddressActivity.this,
								true, "添加成功");
						finish();
					} else if (info != null && !info.trim().equals("")) {
						MyToast.showHMToast(JfMallAddNewAddressActivity.this,
								true, info);
						AppBasic.getInstance().finishActivity(
								JfMallAddNewAddressActivity.this);
					} else {
						MyToast.showHMToast(JfMallAddNewAddressActivity.this,
								false, "添加失败");
					}
				}
			}
			break;
		case POST_EDIT_ADDRESS:
			DtoInfoNoPage<InfoDto> dtoInfoNoPage2 = new DtoInfoNoPage<InfoDto>();
			Type type2 = new TypeToken<DtoInfoNoPage<InfoDto>>() {}.getType();
			dtoInfoNoPage2 = gson.fromJson(msg, type2);
			if(dtoInfoNoPage2 != null) {
				String backCode = String.valueOf(dtoInfoNoPage2.suc);
				if (backCode != null && !backCode.equals("0")
						&& "1011".equals(backCode)) {
					MyToast.showHMToast(JfMallAddNewAddressActivity.this, true,
							"修改成功");
					AppBasic.getInstance().finishActivity(
							JfMallAddNewAddressActivity.this);
				} else {
					Log.e("JfMallAddNewAddressActivity","errorMsg:"+msg);
					MyToast.showHMToast(JfMallAddNewAddressActivity.this, false,
							"修改失败");
				}
			}
			break;
		default:
			break;
		}

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

	@Override
	protected void setParams() {
		home_right.setText("保存");
		home_right.setBackgroundResource(R.drawable.new_address_selector);
		home_right.setWidth(Utils.screenWidth_/9);
		home_right.setHeight(Utils.screenHeight_/20);
		home_right.setGravity(Gravity.CENTER);
		home_right.setTextColor(Color.WHITE);
		home_right.setTextSize(18);
		home_title_txt.setText("创建地址");
		
		addAddressTitle.setOnTouchListener(touch);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			editAddress = (Address) bundle.get("address");
			if (editAddress != null) {
				newRecName.setText(editAddress.addressee);
				newRecCity.setText(editAddress.province + editAddress.city
						+ editAddress.area);
				recPhone.setText(editAddress.cellPhoneNumber);
				newDetailAddress.setText(editAddress.address);
				recFixPhone.setText(editAddress.fixedTelephone);
				home_title_txt.setText("编辑地址");
			}
		}
		address = new Address();
		addressPopWindow = new WheelAddressPopupwindow(
				JfMallAddNewAddressActivity.this, popListener, 0, 0);
		addressPopWindow.setFocusable(true);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {
		String name = newRecName.getText().toString();
		String city = newRecCity.getText().toString();
		String phoneNum = recPhone.getText().toString();
		String detailAddress = newDetailAddress.getText().toString();
		String fixPhoneNum = recFixPhone.getText().toString();
		if (TextUtils.isEmpty(name)) {
			MyToast.showHMToast(JfMallAddNewAddressActivity.this, false,
					"请先填写姓名");
		} else if (TextUtils.isEmpty(city)) {
			MyToast.showHMToast(JfMallAddNewAddressActivity.this, false,
					"请先选择城市");
		} else if (TextUtils.isEmpty(detailAddress)) {
			MyToast.showHMToast(JfMallAddNewAddressActivity.this, false,
					"请先填写详细地址");
		} else if (TextUtils.isEmpty(phoneNum)) {
			MyToast.showHMToast(JfMallAddNewAddressActivity.this, false,
					"请先填写手机号码");
		} else if (TextUtils.isEmpty(newRecCity.getText())) {
			MyToast.showHMToast(JfMallAddNewAddressActivity.this, false,
					"请先选择城市");
		} else if(!Utils.MobilePattern.matcher(phoneNum).matches()){   
			MyToast.showHMToast(JfMallAddNewAddressActivity.this, false,
					"请填写正确的手机号码");
		}else {
			RequestParams params = new RequestParams();
			params.addBodyParameter("addressee", name); // 收件人
			params.addBodyParameter("address", detailAddress);
			params.addBodyParameter("cellPhoneNumber", phoneNum);
			if (!TextUtils.isEmpty(fixPhoneNum)) {
				params.addBodyParameter("fixedTelephone", fixPhoneNum);
			}
			if (editAddress != null) {
				params.addBodyParameter("province", editAddress.province);
				params.addBodyParameter("city", editAddress.city);
				params.addBodyParameter("area", editAddress.area);
				params.addBodyParameter("itemId", editAddress.id);
				onRequest(POST_EDIT_ADDRESS, UrlConstant.EDIT_ADDRESS,
						HttpMethod.POST, params);
			} else {
				params.addBodyParameter("province", address.province);
				params.addBodyParameter("city", address.city);
				params.addBodyParameter("area", address.area);
				onRequest(POST_NEW_ADDRESS, UrlConstant.ADD_ADDRESS,
						HttpMethod.POST, params);
			}

		}
	}

	@Override
	public void onSelected(String province, String city, String district) {
		address.province = province;
		address.city = city;
		address.area = district;
		newRecCity.setText(address.province + address.city + address.area);
	}
}
